define(["require", "exports", "./utils"], function (require, exports, utils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.handleSubmitForm = handleSubmitForm;
    exports.setElementValue = setElementValue;
    exports.getStorageValue = getStorageValue;
    exports.serializeForm = serializeForm;
    function closeSelf() {
        if (parent) {
            const dialog = parent.document.querySelector("#nodePropDialog");
            dialog.querySelector("iframe").src = "./view/blank.html";
            dialog.close();
            const mask = parent.document.querySelector(".mask");
            mask.style.display = "none";
        }
        else {
            window.close();
        }
    }
    let orginFlag = "";
    const params = (0, utils_1.getUrlParams)();
    document.querySelector("form .quit")?.addEventListener("click", () => {
        closeSelf();
    });
    document.querySelector("form")?.addEventListener("submit", (evt) => {
        evt.preventDefault();
    });
    function handleSubmitForm(nodeFlag, handle, autoclose) {
        orginFlag = nodeFlag;
        if (typeof handle != "function") {
            handle = (el) => {
                const data = serializeForm(el);
                saveValue(nodeFlag, JSON.stringify(data));
            };
        }
        document.querySelector("form .ok")?.addEventListener("click", () => {
            const flagVal = document.querySelector("#flag")?.value;
            if (!flagVal) {
                alert("请输入节点标识");
                return;
            }
            const obj = parent.document.querySelector(`[data-flag='${flagVal}']`);
            if (obj && orginFlag != flagVal) {
                alert("节点标识重复");
                return;
            }
            handle(document.querySelector("form"));
            if (autoclose != false) {
                closeSelf();
            }
        });
    }
    function setElementValue(el, paramName) {
        paramName = paramName ?? el;
        const obj = document.querySelector("#" + el) ?? document.querySelector(`[name="${el}"]`);
        let val = (params[paramName] ?? getStorageValue(paramName)) || "";
        if (obj) {
            obj.setAttribute("value", val);
        }
        return val;
    }
    function saveValue(name, value) {
        value = value ?? "";
        utils_1.storage.setItem(name, value);
    }
    function getStorageValue(name, storageKey) {
        storageKey = storageKey ?? params["flag"];
        if (storageKey) {
            const str = utils_1.storage.getItem(storageKey);
            if (str) {
                return JSON.parse(str)[name];
            }
        }
        return null;
    }
    function serializeForm(form) {
        const formData = new FormData(form);
        const json = {};
        for (const [key, value] of formData.entries()) {
            if (json.hasOwnProperty(key)) {
                if (!Array.isArray(json[key])) {
                    json[key] = [json[key]];
                }
                json[key].push(value);
            }
            else {
                json[key] = value;
            }
        }
        return json;
    }
});
